package com.example.oom;

import java.util.ArrayList;
import java.util.List;

public class NativeThreadOOM {

    public static void main(String[] args) {
        List<Thread> threads = new ArrayList<>();
        int count = 0;

        try {
            while (true) {
                Thread t = new Thread(() -> {
                    try {
                        Thread.sleep(Long.MAX_VALUE); // 保持线程存活
                    } catch (InterruptedException ignored) {}
                });
                t.start();
                threads.add(t);
                count++;

                if (count % 100 == 0) {
                    System.out.println("已创建线程数量: " + count);
                }
            }
        } catch (OutOfMemoryError e) {
            System.err.println("触发 unable to create new native thread OOM!");
            e.printStackTrace();
            System.out.println("总共创建线程: " + count);
        }
    }
}
